package sg.nus.iss.vmcs.gui.elements;

import java.awt.Label;
import java.awt.event.ActionListener;

import sg.nus.iss.vmcs.models.Store;
import sg.nus.iss.vmcs.models.StoreItem;
import sg.nus.iss.vmcs.models.StoreObject;
import sg.nus.iss.vmcs.utils.Constant;

/**
 * Builds a StoreViewer Populated with Store Items
 * Builds using ButtonItems
 * 
 * @author Philip
 */
public class ButtonItemStoreViewerBuilder extends StoreViewerBuilder {
	
	//Standard Items required for a StoreViewer
	protected String bannerText;
	protected Store store;
	protected ActionListener listener;
	
	public ButtonItemStoreViewerBuilder(String bannerText, ActionListener listener, Store store) {
		this.bannerText = bannerText;
		this.store = store;
		this.listener = listener;
	}
	
	@Override
	public void buildBanner() {
		Label lblBanner = new Label();
		lblBanner.setText(bannerText);
		lblBanner.setFont(Constant.LABEL_FONT);
		
		storeViewer.setBanner(lblBanner);
	}

	@Override
	public void buildStoreViewerItems() {
		StoreItem[] storeItems = store.getItems();
		ButtonItem[] items = new ButtonItem[storeItems.length];

		for(int i = 0; i < items.length; i++) {
			StoreObject obj = storeItems[i].getContent();
			ButtonItem bi = new ButtonItem(obj.getName(), new GridItemDisplayStrategy());
			bi.setIndex(i);
			bi.setVariableDisplayState(VariableDisplayState.getDisplayState(VariableDisplayState.Type.BIOffState));
			bi.setDisplay();
			bi.setValue(storeItems[i].getQuantity());
			bi.setActionCommand(String.valueOf(i));
			storeItems[i].registerObserver(bi);
			items[i] = bi;
		}
		
		storeViewer.setStoreViewerItems(items);
	}

	/* (non-Javadoc)
	 * @see sg.nus.iss.vmcs.gui.elements.StoreViewerBuilder#buildListeners()
	 */
	@Override
	public void buildListeners() {
		storeViewer.setListener(listener);
	}
}
